﻿using System;
using Kioskvalt.MidiSequencer;
using Kioskvalt.Common;
using System.Globalization;

namespace Kioskvalt.Server
{
    class Program
    {
        // 		public Song m_CurrentSong;

        static void Main(string[] args)
        {
            Notifications.Init();
            SessionManager.Init();

            Configuration.Current = System.Configuration.ConfigurationSettings.GetConfig("kioskvalt") as Configuration;

            MidiWrapper.Start();

            State.CurrentSong = new Song();
            //			State.CurrentSong = MidiSequencer.Song.CreateDummySong();
            State.CurrentSong.Reset();
            State.CurrentSong.LoadFromFile("last.xml");
            for (int j = 0; j < State.CurrentSong.Tracks.Count; j++)
                State.CurrentSong.Tracks[j].CuedPatternID = State.CurrentSong.Tracks[j].LivePatternID;

            State.Sequencer = new MasterSequencer {CurrentSong = State.CurrentSong};

            SeqThread seqthread = new SeqThread();
            WebServerInterface server = new WebServerInterface();

            Console.WriteLine("Starting server on " + Configuration.Current.ServerHost + ":" + Configuration.Current.ServerPort);
            server.Start(Configuration.Current.ServerHost, Configuration.Current.ServerPort);
            seqthread.Start();

            Console.ReadLine();


            seqthread.Stop();
            server.Stop();
            MidiWrapper.Stop();

            State.CurrentSong.SaveToFile("autosave-" + DateTime.Now.ToString("yyyyMMdd-hhiiss", CultureInfo.InvariantCulture) + ".xml");
            State.CurrentSong.SaveToFile("last.xml");

        }

    }
}
